package com.supreme.orderserver.service;

import com.supreme.common.entityVO.CreateOrderVO;
import com.supreme.commonmybatis.entity.primary.MBStorage;
import com.supreme.commonmybatis.mapper.OrderMapper;
import com.supreme.commonmybatis.mapper.StorageMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class StorageService {

    @Resource
    private StorageMapper storageMapper;

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private OrderService orderService;

    public MBStorage createStorage(Long goodsId, Integer goodsNum) {
        final MBStorage storage = new MBStorage()
                .setGoodsId(goodsId)
                .setStockNum(goodsNum)
                .setVersion(1);
        storageMapper.insert(storage);
        return storage;
    }

    @Transactional(rollbackFor = Exception.class)
    public CreateOrderVO processSaleOrder(Long userId, Long goodsId, Integer goodsNum) {
        final CreateOrderVO createOrderVO = new CreateOrderVO();
        createOrderVO.setUserId(userId);
        createOrderVO.setGoodsId(goodsId);
        createOrderVO.setBuyNum(goodsNum);
        orderService.createOrder(createOrderVO);
        return createOrderVO;
    }
}
